package com.sunshine.lnuplus.view.AddLesson;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;

import com.google.android.material.snackbar.Snackbar;
import com.sunshine.lnuplus.R;
import com.sunshine.lnuplus.databinding.ActivityAddLessonBinding;
import com.sunshine.lnuplus.utils.DatabaseHelper;
import com.sunshine.lnuplus.utils.StatusBarUtils;
import com.sunshine.lnuplus.utils.TagHelper;
import com.sunshine.lnuplus.view.BaseActivity;

public class AddLessonActivity extends BaseActivity implements View.OnClickListener {

    private ActivityAddLessonBinding binding;
    private AddLessonViewModel viewModel;
    private AlertDialog colorDialog,weeksDialog;
    private int colorInt = 0;
    private Drawable defaultBackground;
    private boolean[] weeksBoolean;
    private boolean add;
    private String oldLessonName,oldWeeks;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_add_lesson);
        viewModel = ViewModelProviders.of(this).get(AddLessonViewModel.class);
        StatusBarUtils.with(this).setColor(-2).init();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        //接收数据
        Bundle bundle = getIntent().getExtras();
        add = bundle.getBoolean("add");
        if(!add) {
            getSupportActionBar().setTitle("修改课程");
            oldLessonName = bundle.getString("lesson_name");
        }
        binding.editTextAddLessonName.setText(bundle.getString("lesson_name"));
        binding.editTextAddTeacher.setText(bundle.getString("teacher"));
        binding.editTextAddRoom.setText(bundle.getString("room"));
        binding.editTextAddTime.setText(bundle.getString("time"));
        colorInt = Integer.parseInt(bundle.getString("color"));
        oldWeeks = bundle.getString("weeks");
        defaultBackground = binding.buttonCheckColor.getBackground();//获取透明背景
        setColorButtonBackground();//设置选择颜色按钮颜色

        binding.buttonCheckColor.setOnClickListener(this);
        binding.buttonCheckWeeks.setOnClickListener(this);
        binding.fabAddToSave.setOnClickListener(this);
    }

    private void setColorButtonBackground() {
        switch (colorInt){
            case 1:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.red));
                break;
            case 2:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.orange));
                break;
            case 3:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.purple));
                break;
            case 4:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.shallow_green));
                break;
            case 5:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_blue));
                break;
            case 6:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_green));
                break;
            case 7:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.shallow_blue));
                break;
            case 8:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_orange));
                break;
            case 9:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.blue));
                break;
            case 10:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.green));
                break;
            case 11:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_purple));
                break;
            case 12:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.shallow_green2));
                break;
            case 13:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_pink));
                break;
            case 14:
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.shallow_blue2));
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.fab_add_to_save:
                StringBuilder weeks = new StringBuilder();
                int count =0,state = 0;//自定义选择的周数，是否为第一次选择

                //全周
                if(binding.radioButtonAll.isChecked()){
                    weeks.append("1-"+viewModel.getWeeks_count());
                }
                //单双周
                else if(binding.radioButtonDan.isChecked()){
                    for(int i=1;i<=viewModel.getWeeks_count();i++){
                        if(i%2!=0) weeks.append(i+",");
                    }
                }else if(binding.radioButtonShuang.isChecked()){
                    for(int i=1;i<=viewModel.getWeeks_count();i++){
                        if(i%2==0) weeks.append(i+",");
                    }
                }
                //自定义
                else {
                    state = 1;
                    for(int i=0;i<viewModel.getWeeks_count();i++){
                        if(weeksBoolean==null) break;//什么都没有选
                        if(weeksBoolean[i]) {
                            weeks.append((i+1)+",");
                            count++;
                        }
                    }
                }
                if(binding.editTextAddLessonName.getText().toString().equals("")){
                    Snackbar.make(binding.addLessonLayout,"你还没有输入课程名称噢",Snackbar.LENGTH_SHORT).show();
                }else if(count==0&&state==1){
                    if(add) Snackbar.make(binding.addLessonLayout,"你还没有选择周数噢",Snackbar.LENGTH_SHORT).show();
                    else {
                        DatabaseHelper.updateTimetableToDB(this,oldLessonName,binding.editTextAddLessonName.getText().toString(),binding.editTextAddTime.getText().toString(),
                                binding.editTextAddTeacher.getText().toString(),binding.editTextAddRoom.getText().toString(),String.valueOf(colorInt),oldWeeks);
                        setResult(TagHelper.ADDLESSON);
                        finish();
                    }
                }else {
                    if(add) DatabaseHelper.addTimetableToDB(this,binding.editTextAddLessonName.getText().toString(),binding.editTextAddTime.getText().toString(),
                            binding.editTextAddTeacher.getText().toString(),binding.editTextAddRoom.getText().toString(),String.valueOf(colorInt),weeks.toString());
                    else{
                        DatabaseHelper.updateTimetableToDB(this,oldLessonName,binding.editTextAddLessonName.getText().toString(),binding.editTextAddTime.getText().toString(),
                                binding.editTextAddTeacher.getText().toString(),binding.editTextAddRoom.getText().toString(),String.valueOf(colorInt),weeks.toString());
                        DatabaseHelper.updateCountDownToDB(this,oldLessonName,binding.editTextAddLessonName.getText().toString(),null,null,null);
                    }
                    setResult(TagHelper.ADDLESSON);
                    finish();
                }
                break;
            case R.id.button_check_color:
                initColorDialog();
                colorDialog.show();
                break;
            case R.id.button_check_weeks:
                initWeeksDialog();
                weeksDialog.show();
                break;
            case R.id.button_lesson_1:
                colorInt = 1;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.red));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_2:
                colorInt = 2;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.orange));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_3:
                colorInt = 3;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.purple));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_4:
                colorInt = 4;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.shallow_green));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_5:
                colorInt = 5;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_blue));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_6:
                colorInt = 6;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_green));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_7:
                colorInt = 7;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.shallow_blue));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_8:
                colorInt = 8;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_orange));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_9:
                colorInt = 9;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.blue));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_10:
                colorInt = 10;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.green));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_11:
                colorInt = 11;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_purple));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_12:
                colorInt = 12;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.shallow_green2));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_13:
                colorInt = 13;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.hard_pink));
                colorDialog.dismiss();
                break;
            case R.id.button_lesson_14:
                colorInt = 14;
                binding.buttonCheckColor.setBackgroundColor(getResources().getColor(R.color.shallow_blue2));
                colorDialog.dismiss();
                break;
        }
    }

    private void initWeeksDialog() {
        LayoutInflater inflater = LayoutInflater.from(this);
        View dialogView = inflater.inflate(R.layout.dialog_choose_weeks,null);
        final GridView gridView = dialogView.findViewById(R.id.choose_gridview);
        final GridViewAdapter adapter = new GridViewAdapter(this,viewModel.getWeeks_count());
        gridView.setAdapter(adapter);

        ViewGroup viewGroup = (ViewGroup) dialogView.getParent();
        if(viewGroup!=null) viewGroup.removeAllViews();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialogView);
        builder.setTitle("请选择课程周数")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        binding.radioButtonAll.setChecked(false);
                        binding.radioButtonDan.setChecked(false);
                        binding.radioButtonDan.setChecked(false);
                        weeksBoolean = adapter.getBoolean();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        weeksDialog = builder.create();
    }

    private void initColorDialog(){
        LayoutInflater inflater = LayoutInflater.from(this);
        View dialogView = inflater.inflate(R.layout.dialog_choose_lesson_color,null);
        ViewGroup viewGroup = (ViewGroup) dialogView.getParent();
        if(viewGroup!=null) viewGroup.removeAllViews();
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialogView);
        builder.setTitle("请选择课程背景颜色")
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                })
                .setPositiveButton("默认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        colorInt = 0;
                        binding.buttonCheckColor.setBackground(defaultBackground);
                    }
                });
        colorDialog = builder.create();

        //颜色监听
        dialogView.findViewById(R.id.button_lesson_1).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_2).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_3).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_4).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_5).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_6).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_7).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_8).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_9).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_10).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_11).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_12).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_13).setOnClickListener(this);
        dialogView.findViewById(R.id.button_lesson_14).setOnClickListener(this);
    }

    static class GridViewAdapter extends BaseAdapter {

        private Context context;
        private int weeks;
        private boolean[] weeksBoolean;

        public GridViewAdapter(Context context,int weeks){
            this.context = context;
            this.weeks = weeks;
            weeksBoolean = new boolean[weeks];
            for(int i=0;i<weeks;i++) weeksBoolean[i] = true;
        }

        public boolean[] getBoolean(){
            return weeksBoolean;
        }
        @Override
        public int getCount() {
            return weeks;
        }

        @Override
        public Object getItem(int position) {
            return position+1;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService((Context.LAYOUT_INFLATER_SERVICE));
            convertView = layoutInflater.inflate(R.layout.item_dialog_choose_weeks,null);
            final Button button = convertView.findViewById(R.id.choose_weeks_item_button);
            button.setText(String.valueOf(position+1));
            button.setTextColor(context.getResources().getColor(R.color.white));
            if(weeksBoolean[position]){
                button.setBackground(context.getResources().getDrawable(R.drawable.button_theme_pink_bg));
                button.setTextColor(context.getResources().getColor(R.color.white));
            }else button.setTextColor(context.getResources().getColor(R.color.primary_black));
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(weeksBoolean[position]){
                        button.setBackground(context.getResources().getDrawable(R.drawable.button_theme_white_bg));
                        button.setTextColor(context.getResources().getColor(R.color.primary_black));
                        weeksBoolean[position] = false;
                    }else {
                        button.setBackground(context.getResources().getDrawable(R.drawable.button_theme_pink_bg));
                        button.setTextColor(context.getResources().getColor(R.color.white));
                        weeksBoolean[position] = true;
                    }
                }
            });
            return convertView;
        }
    }
}
